package com.kede.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kede.dto.AjaxMessage;
import com.kede.dto.LayuiTableData;
import com.kede.entity.Menu;
import com.kede.entity.SupplyInformation;
import com.kede.service.MenuService;
import com.kede.service.SupplyInformationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 供应信息 前端控制器
 * </p>
 *
 * @author 【yg】
 * @since 2020-04-01
 */
@Controller
@RequestMapping("/supply")
public class SupplyInformationController {

    @Autowired
    MenuService menuService;
    @Autowired
    SupplyInformationService supplyservice;

    @GetMapping({"", "/", "/index"})
    public String index(Model model) {
        List<Menu> rootMenu = menuService.getCurrentMenuListByRoleId();
        model.addAttribute("rootMenuList", rootMenu);
        return "supply/index";
    }


    @ResponseBody
    @GetMapping("/list")
    public LayuiTableData list(Integer page, Integer limit) {

        Page<SupplyInformation> query = new Page<>();
        query.setCurrent(page);
        query.setSize(limit);


        Page<SupplyInformation> pageRlt = supplyservice.page(query);
        LayuiTableData data = new LayuiTableData();
        data.setData(pageRlt.getRecords());
        data.setCount(pageRlt.getTotal());

        return data;
    }

    @ResponseBody
    @PostMapping("save")
    public AjaxMessage save(SupplyInformation supply) {
        AjaxMessage msg = new AjaxMessage();
        supplyservice.updateById(supply);
        return msg;

    }

    @RequiresPermissions("supply/edit")
    @GetMapping("edit/{siid}")
    public ModelAndView edit(@PathVariable("siid") Integer id){


        SupplyInformation supply = supplyservice.getById(id);
        ModelAndView view = new ModelAndView("supply/edit");
        view.addObject("item",supply);
        return view;
    }
}
